/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: data_manager_proxy.hpp
 */

#ifndef _DATA_MANAGER_PROXY_HPP_
#define _DATA_MANAGER_PROXY_HPP_

#include <iostream>
#include <string>

#include "task_data.hpp"
#include "proxy_exception.hpp"

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
/*! \struct DataProxyConfig
 * \brief Configuration for DataManagerProxy.
 *
 * Configuration structure for DataManagerProxy.
 */
typedef struct {
	/*!
	 * Internet Ip v4 address used to send executable tasks' data to
	 * neighbour peers' Worker.
	 */
	std::string ip_to_w; /* IP used to send to w data */
	/*!
	 * Internet Port used to send executable tasks' data to neighbour peers'
	 * Worker.
	 */
	std::string port_to_w; /* PORT used to send to w data */
	/*!
	 * Internet Ip v4 address used to bind and listen for incoming Data.
	 */
	std::string ip_bind_w; /* IP to bind to in order to listen for data */
	/*!
	 * Internet Port used to bind and listen for incoming Data.
	 */
	std::string port_bind_w; /* PORT to bind to in order to listen for data */
} DataProxyConfig;
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*! \class DataManagerProxy
 * \brief Proxy for communication between Manager and Worker.
 *
 * Network interface used to let Manager and Worker communicate in order
 * to send and receive Data of executable tasks. It is important to notice
 * that constructor is private in order to let a factory perform instantiation
 * of an object of this class.
 */
class DataManagerProxy : public TaskData {
	/*!
	 * To let factory build properly this object, we provide access to every
	 * part of it.
	 */
	friend class DataProxyFactory;
public:
	/*!
	 * Destructor.
	 */
	~DataManagerProxy();
	/*!
	 * Sends Data to the specified Worker. This function keeps into consideration
	 * the possibility that the destination Worker might be unavailable
	 * for some reason. For this reason, 10 attempts are performed trying to
	 * connect to the Worker. After 10 failures sending procedure is aborted
	 * and an exception is thrown.
	 * \param data Data to send.
	 * \exception ProxyException Error during connection establishment. this
	 * exception is also thrown when 10 attempts to connect to destination
	 * Worker failed and sending is aborted.
	 */
	void send_data(Data data) throw(ProxyException);
	/*!
	 * Receive data from a Manager.
	 * \return Returns received data.
	 * \exception ProxyException Error during binding.
	 */
	Data recv_data() throw(ProxyException);
private:
	/*!
	 * Privately constructs the object. Default configuration with local
	 * loopback addresses and invalid ports.
	 */
	DataManagerProxy();
	/*!
	 * Privately constructs the object. Assigning configuration.
	 */
	explicit DataManagerProxy(DataProxyConfig conf);
	/*!
	 * Parameters for proxy.
	 */
	DataProxyConfig _config;
}; /* DataManagerProxy */
} /* middleware */

#endif
